import 'package:flutter_boost/flutter_boost.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/model/t3_poi_location.dart';
import 'package:t3_data_service/biz_data_source/map/poi/lat_lng.dart';
import 'package:t3_data_service/data_provider/live_location/t3_live_location_provider.dart';
import 'address_select.dart';

export 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';

export 'poi_search/page/poi_search_page.dart';
export 'map_selected/page/map_selected_poi_page.dart';

export 'configs/address_select_enums.dart';
export 'configs/map_select_config.dart';
export 'configs/poi_search_single_point_config.dart';
export 'configs/poi_search_multi_points_config.dart';

export 'poi_search/page/poi_search_channel.dart';

Future<dynamic> openSinglePointPOISearchPage(POISearchSinglePointConfig config,
    {bool isPresent = true}) async {
  var json = config.toJson();
  if (isPresent) {
    json['isPresentAsPush'] = true;
  }
  var result = BoostNavigator.instance.push(
    'poiSearchSinglePointPage',
    arguments: json,
    withContainer: true,
  );
  print("callSelectAddress--- 起点4");
  return result;
}

Future<dynamic> openMultiPointPOISearchPage(POISearchMultiPointsConfig config,
    {bool isPresent = true}) async {
  var json = config.toJson();
  if (isPresent) {
    json['isPresentAsPush'] = true;
  }
  var result = BoostNavigator.instance.push(
    'poiSearchMultiPointsPage',
    arguments: json,
    withContainer: true,
  );
  print("callSelectAddress--- 起点5");
  return result;
}

Future<dynamic> nativeCallFlutterSelectAddress(dynamic arguments) async {
  print("callSelectAddress--- nativeCallFlutterSelectAddress: $arguments");
  if (arguments is Map) {
    print("callSelectAddress--- arguments: $arguments");
    var liveLocation = T3LiveLocationProvider.instance.liveLocation ??
        T3LiveLocationProvider.instance.pinLocation;
    T3POILocation? location;
    if (liveLocation != null &&
        liveLocation.latitude != null &&
        liveLocation.longitude != null &&
        liveLocation.city != null &&
        liveLocation.adCode != null) {
      location = T3POILocation(
          adCode: liveLocation!.adCode!,
          cityName: liveLocation!.city!,
          coordinate:
              LatLng(liveLocation!.latitude, liveLocation!.longitude));
    }

    if (arguments['type'] != null && arguments['type'] == 1) {
      // 起点
      print("callSelectAddress--- 起点");
      final config = POISearchSinglePointConfig.startNormal(location: location);
      config.autoOpenCitySelection = false;
      config.shouldCheckTableSelectedPoi = false;
      config.entryOptions = [T3EntryOptions.homeAndCompany, T3EntryOptions.map];
      print("callSelectAddress--- 起点1");
      return await openSinglePointPOISearchPage(config);
    } else if (arguments['type'] != null && arguments['type'] == 2) {
      // 终点
      if (location == null) {
        return null;
      }
      print("callSelectAddress--- 起点2");
      final config = POISearchMultiPointsConfig.endNormal(location: location);
      config.originCoor =
          LatLng(location.coordinate.latitude, location.coordinate.longitude);
      config.needHideAddMidwayButton = true;
      print("callSelectAddress--- 起点3");
      return await openMultiPointPOISearchPage(config);
    }
  } else {
    print("callSelectAddress--- 类型错误");
    return null;
  }
}
